const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabledCaptcha: false,
    captchaText: '获取验证码',
    newsPasswordVal:'',
    confirmNewsPasswordVal:'',
    phoneVal:'',
    captchVal:'',
    siteLogoUrl:'',
    phoneText:'',
    phoneFocusBool:'',
    capText:'',
    capTextBool:"",
    newsPwText:'',
    newsPwBool:'',
    confirmPwText:'',
    confirmPwBool:'',
    confirmPwShow:false,
    newsPwShow:false,
  },

  // 获取验证码
  captchaBtn: function () {
    var that = this;
    var time = 10;
    var interval = null;
    var { phoneVal } = this.data;
    if (phoneVal == '') {
      this.setData({
        phoneText:'请输入手机号'
      })
      return false
    }

    if (!(/^1[3-9]\d{9}$/.test(phoneVal))) {
      this.setData({
        phoneText:'手机号格式错误'
      })
      return false
    }
    if(phoneVal){this.setData({phoneText:''})}

    that.setData({
      captchaText: '10秒后重发',
      disabledCaptcha: true
    })
    interval = setInterval(function () {
      time--;
      if (time > 0) {
        that.setData({
          captchaText: time + '秒后重发'
        })
      } else {
        clearInterval(interval);
        that.setData({
          captchaText: '获取验证码',
          disabledCaptcha: false
        })
      }
    }, 1000);
    wx.request({
      url: app.globalData.baseUrl + 'user/sendCode',
      data: {
        phone: phoneVal,
        type: 2
      },
      success(res) {
        console.log(res)
        if (res.data.code == 1000) {
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 下一步
  findPasswordBtn:function(){
    let { phoneVal, captchVal, newsPasswordVal, confirmNewsPasswordVal } = this.data;
    if (phoneVal == '') {
      this.setData({
        phoneText:'请输入手机号'
      })
      return false
    }
    if(phoneVal){this.setData({phoneText:''})}

    if (captchVal == '') {
      this.setData({
        capText:'请输入验证码'
      })
      return false
    }
    if(captchVal){this.setData({capText:''})}

    if (newsPasswordVal == '') {
      this.setData({
        newsPwText:'请输入新密码'
      })
      return false
    }
    if(newsPasswordVal){this.setData({newsPwText:''})}

    if (confirmNewsPasswordVal == '') {
      this.setData({
        confirmPwText:'请再次输入新密码'
      })
      return false
    }
    if(confirmNewsPasswordVal){this.setData({confirmPwText:''})}

    if(newsPasswordVal!==confirmNewsPasswordVal){
      this.setData({
        newsPwText:'两次输入的密码不一致',
        confirmPwText:'两次输入的密码不一致'
      })
      return false
    }
    this.setData({
      newsPwText:'',
      confirmPwText:''
    })


    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/passwordReset',
      data: {
        phone: phoneVal,
        code: captchVal,
        password: newsPasswordVal,
        repassword: confirmNewsPasswordVal,
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.code == 1000) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
            wx.redirectTo({
              url: '/userPage/pages/login/login',
            })
          },2000)
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })



   /* wx.request({
      url: app.globalData.baseUrl + 'user/vertify',
      data: {
        phone: phoneVal,
        code: captchVal
      },
      success(res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.code == 1000) {
          wx.navigateTo({
            url: '/pages/findPasswordConfirm/findPasswordConfirm?token=' + res.data.data.token,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })*/
  },

  newsPwShowCon(){
    this.setData({
      newsPwShow:!this.data.newsPwShow
    })
  },

  confirmPwShowCon(){
    this.setData({
      confirmPwShow:!this.data.confirmPwShow
    })
  },

  goLogin(){
    wx.navigateBack({
      delta: 1
    })
  },


  boolFocus(e){
    const {name}=e.currentTarget.dataset
    this.setData({
      [name]:true
    })
  },

  boolBlus(e){
    const {name}=e.currentTarget.dataset
    this.setData({
      [name]:false
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.siteLogo()
  },


  siteLogo(){
    let _this=this
    wx.request({
      url: app.globalData.baseUrl + 'siteName',
      data: {},
      success(res) {
        if (res.data.code == 1000) {
          _this.setData({
            siteLogoUrl: res.data.data.site_logo_1
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
